# -*- coding = utf-8 -*-
# @Time    : 2025/5/23 下午5:57
# @Author  : yqk
# @File    : url编码.py
# @Software: PyCharm
import urllib.parse

# (1) 值编码
# x = "!"
# x = "#"
# x = "苑"
# # url编码
# ret = urllib.parse.quote(x)
# print(ret)


# (2) 将字典转为urlencoded格式字符串
data = {
    "wd": "美女",
    "page": 1
}
ret = urllib.parse.urlencode(data)
print(ret)  # "wd=%E7%BE%8E%E5%A5%B3&page=1"

data2 = {
    "device_platform": "webapp",
    "aid": 6383,
    "channel": "channel_pc_web",
    "sec_user_id": "MS4wLjABAAAAQERLUS1XLl1qZMZDkibRWUdHGBAoG0pJq_5hAj3XjIZXnxgtW_CcE17nuHHfikpQ",
    "max_cursor": 0,
    "locate_query": "false",
    "show_live_replay_strategy": 1,
}

ret2 = urllib.parse.urlencode(data2)
print(ret2) # device_platform=webapp&aid=6383&channel=channel_pc_web&sec_user_id=MS4wLjABAAAAQERLUS1XLl1qZMZDkibRWUdHGBAoG0pJq_5hAj3XjIZXnxgtW_CcE17nuHHfikpQ&max_cursor=0&locate_query=false&show_live_replay_strategy=1


# (3) 将urlencoded格式字符串转为字典

res = "device_platform=webapp&aid=6383&channel=channel_pc_web&sec_user_id=MS4wLjABAAAAQERLUS1XLl1qZMZDkibRWUdHGBAoG0pJq_5hAj3XjIZXnxgtW_CcE17nuHHfikpQ&max_cursor=0&locate_query=false&show_live_replay_strategy=1"

print(urllib.parse.parse_qs(res))
print(urllib.parse.parse_qsl(res))
